package com.infore.metadata.core.service;

import com.infore.metadata.core.model.ColumnModel;
import com.infore.metadata.core.model.FullyQualifiedJavaType;

import java.sql.Types;

/**
 * 字段业务
 *
 * @author ：lijh
 * @date 创建时间： 2022/4/24 10:09
 */
public class ColumnModelService {

    /**
     * 是否 blob 类型
     * blob适用于非文本数据,图片、视频、pdf文件、MP3文件等等,BLOB是二进制大对象,按字节存储
     * @param jdbcTypeName {@link ColumnModel#getJdbcTypeName()}
     * @return true 是该类型，false 非该类型
     */
    public boolean isBlobColumn(String jdbcTypeName){
        return "BINARY".equalsIgnoreCase(jdbcTypeName) || "BLOB".equalsIgnoreCase(jdbcTypeName)
                || "CLOB".equalsIgnoreCase(jdbcTypeName) || "LONGNVARCHAR".equalsIgnoreCase(jdbcTypeName)
                || "LONGVARBINARY".equalsIgnoreCase(jdbcTypeName) || "LONGVARCHAR".equalsIgnoreCase(jdbcTypeName)
                || "NCLOB".equalsIgnoreCase(jdbcTypeName) || "VARBINARY".equalsIgnoreCase(jdbcTypeName);
    }

    /**
     * 是否字符串类型，这里的字符串类型指 java的String类型
     * @return true 是该类型，false 非该类型
     */
    public boolean isStringColumn(FullyQualifiedJavaType fullyQualifiedJavaType) {
        return fullyQualifiedJavaType.equals(FullyQualifiedJavaType.getStringInstance());
    }

    /**
     * 是否字符串类型，这里的字符串类型指数据库的
     * @param jdbcType  {@link ColumnModel#getJdbcType()}
     * @return true 是该类型，false 非该类型
     */
    public boolean isJdbcCharacterColumn(Integer jdbcType) {
        return jdbcType == Types.CHAR || jdbcType == Types.CLOB
                || jdbcType == Types.LONGVARCHAR || jdbcType == Types.VARCHAR
                || jdbcType == Types.LONGNVARCHAR || jdbcType == Types.NCHAR
                || jdbcType == Types.NCLOB || jdbcType == Types.NVARCHAR;
    }

    /**
     * 是否时间类型-java的时间类型
     * @param jdbcTypeName {@link ColumnModel#getJdbcTypeName()}
     * @return true 是该类型，false 非该类型
     */
    public boolean isJDBCDateColumn(FullyQualifiedJavaType fullyQualifiedJavaType, String jdbcTypeName) {
        return fullyQualifiedJavaType.equals(FullyQualifiedJavaType.getDateInstance()) && "DATE".equalsIgnoreCase(jdbcTypeName);
    }

    /**
     * 是否时间类型-java的时间类型
     * @param jdbcTypeName {@link ColumnModel#getJdbcTypeName()}
     * @return true 是该类型，false 非该类型
     */
    public boolean isJDBCTimeColumn(FullyQualifiedJavaType fullyQualifiedJavaType, String jdbcTypeName) {
        return fullyQualifiedJavaType.equals(FullyQualifiedJavaType.getDateInstance()) && "TIME".equalsIgnoreCase(jdbcTypeName);
    }
}
